package com.lh.controller;

import com.lh.pojo.Items;
import com.lh.pojo.ItemsImg;
import com.lh.pojo.ItemsParam;
import com.lh.pojo.ItemsSpec;
import com.lh.pojo.vo.CommentLevelVO;
import com.lh.pojo.vo.ItemInfoVO;
import com.lh.pojo.vo.ShopCartVO;
import com.lh.service.ItemService;
import com.lh.utils.IMOOCJSONResult;
import com.lh.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: foodie-dev
 * @description:
 * @author: LH
 * @create: 2022-05-07 14:37
 **/
@Api(value = "商品详情相关接口", tags = {"商品详情相关api接口"})
@RestController
@RequestMapping("/items")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "商品详情", notes = "商品详情", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/info/{itemId}")
    public IMOOCJSONResult getSixNewItems(
            @ApiParam(name = "商品Id", value = "商品Id", required = true)
            @PathVariable String itemId){

        if (StringUtils.isEmpty(itemId)){
            IMOOCJSONResult.errorMsg("商品Id为空!");
        }

        ItemInfoVO itemInfoVO = new ItemInfoVO();

        Items item = itemService.queryItem(itemId);
        List<ItemsImg> itemImgList = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemSpecList = itemService.queryItemSpecList(itemId);
        ItemsParam itemParams = itemService.queryItemParam(itemId);

        itemInfoVO.setItem(item);
        itemInfoVO.setItemImgList(itemImgList);
        itemInfoVO.setItemSpecList(itemSpecList);
        itemInfoVO.setItemParams(itemParams);

        return IMOOCJSONResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "商品评价等级数量", notes = "商品评价等级数量", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/commentLevel")
    public IMOOCJSONResult getCommentLevelNum(
            @ApiParam(name = "商品Id", value = "商品Id", required = true)
            @RequestParam String itemId
    ){
        if (StringUtils.isEmpty(itemId)){
            IMOOCJSONResult.errorMsg("商品Id为空!");
        }

        CommentLevelVO commentLevelNum = itemService.getCommentLevelNum(itemId);
        return IMOOCJSONResult.ok(commentLevelNum);
    }

    @ApiOperation(value = "商品评价详情", notes = "商品评价详情", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/comments")
    public IMOOCJSONResult getComments(
            @ApiParam(name = "商品Id", value = "商品Id", required = true)
            @RequestParam String itemId,
            @ApiParam(name = "评论level", value = "评论level", required = false)
            @RequestParam Integer level,
            @ApiParam(name = "页码", value = "页码", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "每页数目", value = "每页数目", required = false)
            @RequestParam Integer pageSize
    ){
        if (StringUtils.isEmpty(itemId)){
            IMOOCJSONResult.errorMsg("商品Id为空!");
        }

        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }

        PagedGridResult result = itemService.getComments(itemId, level, page, pageSize);

        return IMOOCJSONResult.ok(result);
    }


    @ApiOperation(value = "根据关键字搜索商品", notes = "根据关键字搜索商品", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/search")
    public IMOOCJSONResult searchItemsByKeywords(
            @ApiParam(name = "搜索关键字", value = "搜索关键字", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "排序", value = "排序", required = true)
            @RequestParam String sort,
            @ApiParam(name = "页码", value = "页码", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "每页数目", value = "每页数目", required = false)
            @RequestParam Integer pageSize
    ){
        if (StringUtils.isEmpty(keywords)){
            IMOOCJSONResult.errorMsg("搜索关键字为空!");
        }
        if (StringUtils.isEmpty(sort)){
            IMOOCJSONResult.errorMsg("排序方式为空!");
        }
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = 20;
        }

        PagedGridResult pagedGridResult = itemService.searchItemsByKeywords(keywords, sort, page, pageSize);

        return IMOOCJSONResult.ok(pagedGridResult);
    }

    @ApiOperation(value = "根据商品分类搜索商品", notes = "根据商品分类搜索商品", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/catItems")
    public IMOOCJSONResult searchItemsByKeywords(
            @ApiParam(name = "商品分类Id", value = "商品分类Id", required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "排序", value = "排序", required = true)
            @RequestParam String sort,
            @ApiParam(name = "页码", value = "页码", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "每页数目", value = "每页数目", required = false)
            @RequestParam Integer pageSize
    ){
        if (catId == null){
            IMOOCJSONResult.errorMsg("商品分类Id为空!");
        }
        if (StringUtils.isEmpty(sort)){
            IMOOCJSONResult.errorMsg("排序方式为空!");
        }
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = 20;
        }

        PagedGridResult pagedGridResult = itemService.searchItemsByCatId(catId, sort, page, pageSize);

        return IMOOCJSONResult.ok(pagedGridResult);
    }

    @ApiOperation(value = "刷新购物车商品规格", notes = "刷新购物车商品规格", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/refresh")
    public IMOOCJSONResult refreshShopCart(
            @ApiParam(name = "页码", value = "页码", required = false)
            @RequestParam String [] itemSpecIds
    ){
        if (itemSpecIds == null || itemSpecIds.length == 0){
            return IMOOCJSONResult.errorMsg("商品规格为空!");
        }

        List<String> specIdList = new ArrayList<>();
        for (int i = 0; i < itemSpecIds.length; i++) {
            specIdList.add(itemSpecIds[i]);
        }

        List<ShopCartVO> shopCartVOList = itemService.refreshShopCart(specIdList);

        return IMOOCJSONResult.ok(shopCartVOList);
    }

}
